// pages/welfareMall/cart/order/index.js
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //服务器图片的路径片段
    imgUrl:app.globalData.iconImgUrl,
    StatusBar: app.globalData.StatusBar,   //状态栏高度
    CustomBar: app.globalData.CustomBar,  //头部nav高度
    backIcon: true,   // 返回按钮是否显示
    title: "填写订单",  //页面标题
    backImg: app.globalData.iconImgUrl+"arrow-left.svg",
    cartList: [],
    region: ['广东省', '广州市', '海珠区'],
    customItem: '全部',
    totalPrice: 0,
    currentPage: 1,
    pageSize: 8,
    totalPage: 1,
    totalYD: 0,
    payVisible: false,
    consignee: '',
    detailAddress: '',
    area: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options.idArr.split(","))
    this.setData({
      shopcartList: options.idArr.split(","),
      type: options.type
    })
    // console.log(this.data.shopcartList)
    this.getList()
  },
  getList() {
    const that = this
    const data = {
      "userCode": wx.getStorageSync('userCode'),
      "currentPage": this.data.currentPage,
      "pageSize": this.data.pageSize,
      "shopcartList": this.data.shopcartList,
      "type": this.data.type
    }
    // console.log(data)
    console.log('idArr', this.data.shopcartList)
    wx.request({
      url: app.globalData.baseUrl + 'shopcard/goPayInfo.do',
      data: data,
      method: 'post',
      success(res) {
        // console.log('商品列表', res)
        that.setData({
          cartList: res.data.productList,
          totalPrice: res.data.totalPrice,
          totalYD: res.data.totalYD
        })
      }
    })
  },
  pay() {
    const that = this
    console.log(that.data.shopcartList)
    const data = {
      "userCode": wx.getStorageSync('userCode'),
      "consignee": this.data.consignee,
      "mobileNo": this.data.phone * 1,
      "area": this.data.region.join(''),
      "totalYD": this.data.totalYD,
      "detailAddress": this.data.detailAddress,
      "shopcartList": this.data.shopcartList
    }

    console.log('点歌单', data)
    
    wx.request({
      url: app.globalData.baseUrl + 'shopcard/goPay.do',
      method: 'post',
      data: data,
      success(res) {
        console.log(res)
        if (res.data.isSuccess) {
          wx.showToast({
            title: '下单成功',
            success() {
              that.hidePay()
              that.updateCartNum()  
            }
          })
        }
        if (res.data.errorMsg === "您的誉点不足，请获取足够的誉点后再进行付款!") {
          wx.showToast({
            title: '您的誉点不足',
            image: '/assets/images/icon/error.png'
          })
        }
      }
    })
  },
  updateCartNum() {
    var pages = getCurrentPages();//当前页面栈
    var mallIndex = pages[0]
    var timer = null
    mallIndex.getCartMes()
    
    timer = setTimeout(function() {
      wx.navigateBack({
        delta: pages.length
      })
      clearTimeout(timer)
    }, 3000)
    
  },
  detailAddressChange(e) {
    this.setData({
      detailAddress: e.detail.value
    })
  },
  phoneChange(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  consigneeChange(e) {
    this.setData({
      consignee: e.detail.value
    })
  },
  showPay() {
    const consignee = this.data.consignee;
    const mobileNo = this.data.phone;
    const detailAddress = this.data.detailAddress;
    if (consignee !== '' && mobileNo !== '' && detailAddress !== '') {
      this.setData({
        payVisible: true
      })
    }else{
      wx.showToast({
        title: '请填写订单信息',
        image: '/assets/images/icon/error.png'
      })
    }
    
  },
  hidePay() {
    this.setData({
      payVisible: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  //省市区选择函数
  bindRegionChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // const shopcartList = this.data.shopcartList
    // if (shopcartList.length > 0) {
    //   this.setData({
    //     shopcartList: [],
    //     currentPage: 1,
    //     totalPage: 1,
    //     cartList: [],
    //     totalPrice: 0,
    //     totalYD: 0
    //   })
    //   this.getList()
    // }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})